<?php
	
	if (!defined('BASEPATH')) {exit('No direct script access allowed');}

class dashboard extends CI_Controller { 
	function __construct(){
		parent::__construct();
		$this->load->library('encrypt');
		$this->load->model('mdata');
		$this->sesina = unserialize(base64_decode($this->session->userdata('ses_data')));
	}
	
	function index(){	
		if($this->sesina){
			$data=$this->sesina;
			$data['modul']='home';
			show('modul/dashboard',$data);
		}
		
		else{
			$data["pesan"]="";
			$data["user"]="";
			$data["pass"]="";
			$this->load->view('login',$data);
		}
	}
		
	function get_pages($p1="",$p2=""){
		if($this->sesina){
			$slot_role = $this->mdata->get_data('slot_propinsi_kab',$p1);
			$data['slot_propinsi']  = $slot_role->slot_propinsi;
			$data['slot_kabupaten'] = $slot_role->slot_kabupaten;
			$data['id_menu']=$p1 ;
			
		
			switch($p1){
				case 1:
					$data['modul'] = "geografi";
				break;
				default:$data['modul'] = $p1;
			}
			
			show('modul/dashboard',$data);
		}else{
			$data["pesan"]="";
			$data["user"]="";
			$data["pass"]="";
			$this->load->view('login',$data);
			exit;
		}
	}
	
	function fillCombo($req, $val=''){
		switch($req){
			case 'prov':
				$data = $this->mdata->fillCombo($req);
				break;
				
			case 'kat':
				$data = $this->mdata->fillCombo('kat', $val);
				break;	
		}
		
		$optTxt = "<option value='0'>".($req =='prov' ? 'SEMUA PROVINSI ( NASIONAL )' : '-- Pilih --')."</option>";  
		if($data){
			foreach($data as $k=>$v)
				$optTxt .= "<option value='".$v['id']."'>".$v['txt']."</option>";
		}
		
		if($val=='-1')
			return $optTxt;
		else echo $optTxt;	
	}
	
	function get_datagrap($p1=""){
		$item_id = $this->input->post('item_data_id');
		$id_jenis = $this->input->post('id_jenis');
		
		$data = $this->mdata->get_data('data_grafik',$item_id, $id_jenis);
		
		$xmlTxt = "<graph baseFont='Arial' baseFontColor='333333' xaxisname='Nama Sub Kategori' yaxisname='Jumlah (*1.000.000)' hovercapbg='DEDEBE' hovercapborder='889E6D' rotateNames='0' yAxisMaxValue='' numdivlines='9' divLineColor='CCCCCC' useRoundEdges='1' bgColor='FFFFFF,FFFFFF' showBorder='0' decimalPrecision='5' caption='' subcaption='' formatNumberScale='0' numberSuffix=''>";
				$xmlCatg = "<categories font='Arial' fontColor='000000'>";
				$xmlReal = "<dataset seriesname='Harga (Rp)' color='56B9F9'>";
				
				foreach($data as $k=>$v){
					
					$xmlTxt .= "<set name='".$v['nama']."' value='".$v['NILAI']."' ";
					$xmlTxt .= "hoverText='".$v['nama'].", Dengan Jumlah : ".number_format($v['NILAI'],5)." ' />";
				};
				
				$xmlCatg .= "</categories>";
				$xmlReal .= "</dataset>";
				$xmlTxt .= "</graph>";
				//$xmlTxt .= $xmlCatg.$xmlthn.$xmlthnsblm."</graph>";
		
			/*$xml="<chart yAxisName='Jumlah (*1.000.000)' showLegend='0' labelDisplay='ROTATE' rotateValues='1' numDivLines='5' slantLabels='1' caption='Data Per Kabupaten' numberPrefix='' showBorder='0' imageSave='1' exportHandler=''>";	
				foreach ($data as $x=>$v){
				//	$xml .='<set label="'.$v['nama'].'" value="'.$v['NILAI'].'" />';
					$xml .= "<set name='".$v['nama']."' value='".$v['NILAI']."' ";
					$xml .= "hoverText='".$v['nama'].", Data : ".number_format($v['NILAI'], 0, ',', '.')." ' />";
				}
			$xml .='</chart>';
		*/
		
		echo $xmlTxt;
	}
	
		function changePass(){
		$oldPass = $_POST['oldPass'];
		$newPass = $this->encrypt->encode($_POST['newPass']);
		//echo $this->encrypt->decode('D2IIPVJmADoCNw==');
		if($this->sesina){
			if($oldPass == $this->encrypt->decode($this->sesina['USER_PASS'])){		
				$this->mdata->changePass($this->sesina['USER_ID'], $newPass);
				echo 'sukses';
			} else {
				echo 'tidak sukses';
			}
		}
	}
	
	
}